package com.campus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.common.core.Pager;
import com.campus.common.core.Result;
import com.campus.common.core.TableData;

import com.campus.model.dto.*;
import com.campus.model.param.AddClassStudentParam;
import com.campus.model.param.AddClassStudentsParam;
import com.campus.model.param.AddClassTeacherParam;
import com.campus.model.vo.ClassPageVo;
import com.campus.service.IManagerClassService;
import com.campus.service.IUserRoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/class")
@Api(tags = "班级管理")
public class ManagerClassController {

    @Autowired
    private IManagerClassService managerClassService;

    @Autowired
    private IUserRoleService roleService;

    @PostMapping("/add")
    @ApiOperation("添加班级")
    public Result<Integer> addClass(@RequestBody @Validated ManagerClassCreateDto managerClassCreateDto) {
        return Result.success(managerClassService.createClass(managerClassCreateDto));
    }

    @PutMapping("/update")
    @ApiOperation("编辑班级")
    public Result<Integer> updateClass(@RequestBody @Validated ManagerClassUpdateDto managerClassUpdateDto) {
        return Result.success(managerClassService.updateClass(managerClassUpdateDto));
    }

    @GetMapping("/list")
    @ApiOperation("班级列表查询")
    public TableData<ClassPageVo> getClassList(ManagerClassConditionDto managerClassConditionDto, Pager pager) {
        Page<ClassPageVo> classListByConditions = managerClassService.getClassListByConditions(managerClassConditionDto, pager);
        return TableData.success(classListByConditions.getRecords(), classListByConditions.getTotal());
    }

    @PutMapping("/addClassTeacher")
    @ApiOperation("添加(修改)班级教师")
    public Result<Integer> addClassTeacher(@RequestBody @Validated AddClassTeacherParam addClassTeacherParam) {

        if (managerClassService.TeacherClassCount(addClassTeacherParam.getTeacherId()) > 2) {
            return Result.error("该教师已存在两个班级中");
        }
        return Result.success(managerClassService.addClassTeacher(addClassTeacherParam.getClassId(),
                addClassTeacherParam.getTeacherId()));

    }

    @PutMapping("/addClassStudents")
    @ApiOperation("批量添加班级学生")
    public Result<Integer> addClassStudents(@RequestBody @Validated AddClassStudentsParam addClassStudentsParam) {

        //检查学生是否已经存在于其他班级 and 检查该用户是否是学生
        if (!managerClassService.isJoinClass(addClassStudentsParam.getStudentIds())) {
            return Result.error("学生已存在其他班级中");
        }
        return Result.success(managerClassService.addClassStudents(addClassStudentsParam));
    }

    @PutMapping("/addClassStudent")
    @ApiOperation("添加班级学生")
    public Result<Integer> addClassStudent(@RequestBody @Validated AddClassStudentParam addClassStudentParam) {

        if (managerClassService.isJoinClass(addClassStudentParam.getStudentId())) {
            return Result.error("学生已存在其他班级中");
        }
        return Result.success(managerClassService.addClassStudent(addClassStudentParam));
    }

}
